/**
 * 自定义函数
 * */

var userLogin = 'userInfo'; //登录信息
var apiMethod = 'post';
var apiDomain = '/api/apk/';
var bomMenuArys = [
    {
        id: 1, title: '地图', link: 'index.html',
    },
    {
        id: 2, title: '扫码', link: 'scan.html',
    },
    {
        id: 3, title: '配置', link: 'set.html',
    }
];


(function () {
    // var path = window.location.pathname;
	// bomMenu();
})();


//退出
function logout(){
    delCache({key: userLogin});
    window.location.href = 'login.html';
}


// 底部菜单
function bomMenu() {
    // var formData = {
    //     uid: 0, role_id: 0,
    // };
    // $.ajax({
    //     type: apiMethod,
    //     url: apiDomain + 'bommenu',
    //     data: formData,
    //     success: function (res) {
    //         console.log(res);
    //         if (res.code != 0) {
    //             msgPop({msg: '权限错误'});
    //             return
    //         }
    //         bomMenuHtml(res.data);
    //     },
    //     error: function () {
    //         console.log('网络错误');
    //     }
    // });
	bomMenuHtml();
}
//底部菜单字符串
function bomMenuHtml(){
    var h = '', i = 0, ms = bomMenuArys;
    for (; i < ms.length; i ++) {
        if (bm_id == ms[i].id) {
            h += '<div class="li click" onclick="toMenu(' + ms[i].id + ');">';
        } else {
            h += '<div class="li" onclick="toMenu(' + ms[i].id + ');">';
        }
        h += ms[i].title;
        h += '</div>';
    }
    $('#bomMenu').html(h);
}
//切换菜单
function toMenu(v){
    console.log(v, bomMenuArys);
    var link = '', ms = bomMenuArys, i = 0;
    for (i; i < ms.length; i ++) {
        if (v == ms[i].id) {
            link = ms[i].link;
        }
    }
    window.location.href = link;
}


//显示字符串前几个字符
function strPrefix(str, n){
    if (str.length <= n) {
        return str;
    } else {
        return str.substring(0, n) + '...';
    }
}


//生成随机数
function randInt(min, max) {
    // 加1是因为我们想要包含max值在内
    return Math.floor(Math.random() * (max - min + 1)) + min;
}


//抓图
function uploadImage() {
    var fileObj = $('#bigUpload')[0],
        reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        //console.log(res.target.result);
        showImg({img: res.target.result});
    }
}


//登录aid
function getAid(){
    var adminInfo = getAdminInfo();
    if (!adminInfo) {
        msgPop({msg: '没有登录'});
        setTimeout(function () {
            location.href = '/ad369/login.html';
        }, 1000);
        return
    }
    return adminInfo.aid;
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');}, 2000)</script>';
    $('#msgPop').html(h);
}


//获取登录信息
function getUserInfo(){
    return getCache(userLogin);
}


//设置登录信息
function setUserInfo(e){
    return setCache({key: userLogin, val: e.val});
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//设置缓存
function setCache(e){
    var key = e.key, val = JSON.stringify(e.val);
    window.localStorage.setItem(key, val);
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//删除指定缓存
function delCache(e){
    var key = e.key;
    window.localStorage.removeItem(key);
}


//清理缓存
function clearCache(){
    window.localStorage.clear();
}